package com.luban.springmybatisluban;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;

public class MapperFactoryBean implements FactoryBean {

    //定义mapper的Class
    private Class mapperClass;

    private SqlSession sqlSession;

    //使用spring-beanDefinition.getConstructorArgumentValues().addGenericArgumentValue()
    public MapperFactoryBean(Class mapperClass) {
        this.mapperClass = mapperClass;
    }


    /**
     * 此处使用的AUTOWIRE_BY_TYPE
     *
     * @param sqlSessionFactory sqlSessionFactory
     */
    public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
        sqlSessionFactory.getConfiguration().addMapper(mapperClass);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    public Object getObject() {
        //此处使用的是mybatis的逻辑
        return sqlSession.getMapper(mapperClass);
    }

    public Class<?> getObjectType() {
        return mapperClass;
    }
}
